package Presentacion;

import Clases.Bono;
import Clases.Empleado;
import Clases.Pago;
import Clases.PagoId;
import Control.ControlBono;
import Control.ControlEmpleado;
import Control.ControlPago;
import de.javasoft.plaf.synthetica.SyntheticaLookAndFeel;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.NumberFormatter;

public class ConsultarBonos extends javax.swing.JFrame {

    /**
     * Variables de la clase.
     */
    ControlBono controlB = ControlBono.obtenerInstancia();
    ControlEmpleado controlE = ControlEmpleado.obtenerInstancia();
    ControlPago controlP = ControlPago.obtenerInstancia();
    private ImageIcon iconoError;
    NumberFormat numberFormat = (DecimalFormat) DecimalFormat.getNumberInstance();
    private static ConsultarBonos instancia = null;

    /**
     * Constructor de la clase que inicializa todos sus elementos.
     */
    public ConsultarBonos() {
        initComponents();
        inicioTooltops();
    }

    /**
     * Método que crea una sola instancia para esta clase.
     */
    public static ConsultarBonos obtenerInstancia() {
        if (instancia == null) {
            instancia = new ConsultarBonos();
        }
        return instancia;
    }

    /**
     * Método que hace visible esta interfaz con sus respectivas propiedades.
     * Asigna también un Look And Feel a la interfaz.
     */
    public void init() {
        setTitle("Manejo de Bonos");
        setLocationRelativeTo(null);
        UIManager.removeAuxiliaryLookAndFeel(UIManager.getLookAndFeel());
        SwingUtilities.updateComponentTreeUI(this);
        try {
            UIManager.removeAuxiliaryLookAndFeel(UIManager.getLookAndFeel());
            SyntheticaLookAndFeel.setWindowsDecorated(false);
            UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaAluOxideLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);

        } catch (Exception ex) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.limpiarVentana();
        this.NumberFormat();
        jTable1.getColumnModel().getColumn(0).setWidth(jTable1.getColumnModel().getColumn(0).getMinWidth() + 10);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(jTable1.getColumnModel().getColumn(0).getMinWidth() + 10);
        this.setVisible(true);
    }

    /**
     * Método que da formato de numero a un campo de texto respectivamente
     * (jTextValor)
     */
    public void NumberFormat() {
        numberFormat.setMaximumIntegerDigits(7);
        numberFormat.setMinimumFractionDigits(0);
        numberFormat.setMinimumIntegerDigits(2);
        NumberFormatter nfr = new NumberFormatter(numberFormat);
        this.jTextValor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(nfr));
    }

    /**
     * Método que se encarga de ocultar los labels que muestran los mensajes de
     * error
     */
    private void ocultarLabels() {
        jLabelDescripcion.setVisible(false);
        jLabelValor.setVisible(false);
        jLabelCedula.setVisible(false);
    }

    /**
     * Método que asigna un tooltip a cada icono de error
     */
    private void inicioTooltops() {
        iconoError = new ImageIcon(getClass().getResource("/Imagenes/warning.png"));

        jLabelDescripcion.setIcon(iconoError);
        jLabelDescripcion.setToolTipText("Debe escribir una descripción.");

        jLabelValor.setIcon(iconoError);
        jLabelValor.setToolTipText("Debe escribir un valor correcto y mayor a mil colones.");

        jLabelCedula.setIcon(iconoError);
        jLabelCedula.setToolTipText("Debe buscar un empleado");
        ocultarLabels();
    }

    public void setEmpleado(String cedulaEmpleado, String nombre, String caso) {
        ControlEmpleado ControlE = ControlEmpleado.obtenerInstancia();
        if (caso.equals("bono1")) {
            this.labelCedulaIngresar.setText(cedulaEmpleado);
            this.labelNombreIngresar.setText(nombre);
            this.labelCodigoI.setText(String.valueOf(ControlE.getEmpleadoCedula(Long.parseLong(cedulaEmpleado)).getCodigo()));
        } else {
            this.labelCedulaManejar.setText(cedulaEmpleado);
            this.labelNombreManejar.setText(nombre);
            this.labelCodigoC.setText(String.valueOf(ControlE.getEmpleadoCedula(Long.parseLong(cedulaEmpleado)).getCodigo()));
            this.llenarTablaAsignacionBonos(Long.valueOf(cedulaEmpleado));
        }

    }

    /**
     * Método que obtiene los bonos seleccionados de la tabla.
     */
    public ArrayList<Long> getBonosSeleccionados() {
        ArrayList<Long> bonos = new ArrayList<Long>();
        for (int i = 0; i < jTable1.getRowCount(); i++) {
            if ((Boolean) jTable1.getValueAt(i, 0).equals(Boolean.TRUE)) {
                bonos.add((Long) jTable1.getValueAt(i, 1));
            }
        }
        return bonos;
    }

    /**
     * Método que llena una tabla de las asgnaciones de bonos de un empleado
     *
     * @param identificador del empleado para bucar respectivamente sus bonos
     * asignados.
     *
     */
    public void llenarTablaAsignacionBonos(Long identificador) {
        ArrayList<Bono> bonos = null;
        ArrayList<Date> fechas = ControlPago.obtenerInstancia().getPago();
        bonos = controlB.getBonosPorCedula(identificador, new java.sql.Date(fechas.get(0).getTime()), new java.sql.Date(fechas.get(1).getTime()));

        DefaultTableModel m = (DefaultTableModel) jTable1.getModel();
        try {
            while (m.getRowCount() > 0) {
                m.removeRow(0);
            }
            for (int i = 0; i < bonos.size(); i++) {
                m.insertRow(m.getRowCount(), new Object[]{false, bonos.get(i).getCodigo(),
                    bonos.get(i).getDescripcion(), "₡ " + ((Integer) bonos.get(i).getValor()).toString()});
            }
        } catch (Exception e) {
            System.out.println(e.getCause());
        }
    }

    /**
     * Método que limpia los elementos de la interfaz.
     */
    public void limpiar() {
        jTextValor.setText("");
        jTextValor.setValue(null);
        jTextDescripcion.setText("");
        labelCedulaIngresar.setText("__________________________");
        labelNombreIngresar.setText("__________________________");
        labelCodigoI.setText("__________________________");

        ocultarLabels();
    }

    /**
     * Método que limpia los elementos de la interfaz, la tabla y ademas agrega
     * lineas al label de cédula,código y nombre del empleado.
     */
    public void limpiarVentana() {
        limpiar();
        labelCedulaManejar.setText("________________________________");
        labelCodigoC.setText("__________________________");
        labelNombreManejar.setText("________________________________");
        DefaultTableModel m = (DefaultTableModel) jTable1.getModel();
        try {
            while (m.getRowCount() > 0) {
                m.removeRow(0);
            }
        } catch (Exception e) {
            System.out.println(e.getCause());
        }
        ocultarLabels();
    }

    /**
     * Método que se encarga de validar que los datos ingresados del bono sean
     * válidos.
     */
    private boolean validarBono() {
        boolean itsOk = true;

        String cedula = labelCedulaIngresar.getText().trim();
        if (Character.isDigit(cedula.charAt(0)) == false) {
            itsOk = false;
            jLabelCedula.setVisible(true);
        }
        String valor = jTextValor.getText();
        try {
            if (valor.equals("")) {
                jLabelValor.setVisible(true);
                itsOk = false;
            } else {
                Integer valorEntero = jTextValor.getText().trim().isEmpty() ? null : numberFormat.parse(jTextValor.getText()).intValue();
                if (valorEntero < 1000) {
                    jLabelValor.setVisible(true);
                    itsOk = false;
                }
            }
        } catch (Exception e) {
            jLabelValor.setVisible(true);
            itsOk = false;
        }

        String descripcion = jTextDescripcion.getText();
        if (descripcion.trim().isEmpty()) {
            itsOk = false;
            jLabelDescripcion.setVisible(true);
        }
        return itsOk;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        labelCedulaManejar = new javax.swing.JLabel();
        labelNombreManejar = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        labelCodigoC = new javax.swing.JLabel();
        jButton9 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextDescripcion = new javax.swing.JTextField();
        jButton7 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabelDescripcion = new javax.swing.JLabel();
        jLabelValor = new javax.swing.JLabel();
        jTextValor = new javax.swing.JFormattedTextField();
        jLabelCedula = new javax.swing.JLabel();
        labelCedulaIngresar = new javax.swing.JLabel();
        labelNombreIngresar = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        labelCodigoI = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(720, 440));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButton1.setText("Buscar Empleado ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "", "Código", "Descripción", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Boolean.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Cédula del empleado: ");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel10.setText("Nombre del empleado:");

        jButton2.setText("Eliminar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        labelCedulaManejar.setText("_________________________________");
        labelCedulaManejar.setFocusable(false);

        labelNombreManejar.setText("_________________________________");
        labelNombreManejar.setFocusable(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Código del empleado:");

        labelCodigoC.setText("__________________________________");

        jButton9.setText("Salir");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10)
                            .addComponent(jLabel2))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelCodigoC, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(labelCedulaManejar, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton1)))
                                .addContainerGap(191, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(labelNombreManejar, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addContainerGap())))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton9)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel9))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelCedulaManejar)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(labelCodigoC))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(labelNombreManejar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton9))
                .addGap(20, 20, 20))
        );

        jTabbedPane1.addTab("Consultar Bonos", jPanel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("Cédula del empleado: ");

        jButton5.setText("Buscar Empleado");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Descripción:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel8.setText("Valor:");

        jButton7.setText("Crear");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Nombre de empleado:");

        jLabelDescripcion.setText("x");
        jLabelDescripcion.setMaximumSize(new java.awt.Dimension(24, 24));
        jLabelDescripcion.setMinimumSize(new java.awt.Dimension(22, 22));
        jLabelDescripcion.setPreferredSize(new java.awt.Dimension(22, 22));

        jLabelValor.setText("x");
        jLabelValor.setMaximumSize(new java.awt.Dimension(24, 24));
        jLabelValor.setMinimumSize(new java.awt.Dimension(22, 22));
        jLabelValor.setPreferredSize(new java.awt.Dimension(22, 22));

        jTextValor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextValorKeyPressed(evt);
            }
        });

        jLabelCedula.setText("x");
        jLabelCedula.setMaximumSize(new java.awt.Dimension(24, 24));
        jLabelCedula.setMinimumSize(new java.awt.Dimension(22, 22));
        jLabelCedula.setPreferredSize(new java.awt.Dimension(22, 22));

        labelCedulaIngresar.setText("________________________");
        labelCedulaIngresar.setFocusable(false);

        labelNombreIngresar.setText("_______________________");
        labelNombreIngresar.setFocusable(false);

        jButton3.setText("Salir");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Código del empleado:");

        labelCodigoI.setText("_______________________");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(93, 93, 93)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextValor, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelValor, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGap(19, 19, 19)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(labelCedulaIngresar, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabelCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(107, 107, 107))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelNombreIngresar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(labelCodigoI, javax.swing.GroupLayout.DEFAULT_SIZE, 523, Short.MAX_VALUE))
                                .addContainerGap())))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(labelCedulaIngresar)
                    .addComponent(jButton5)
                    .addComponent(jLabelCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCodigoI)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNombreIngresar)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton7)
                            .addComponent(jButton3))
                        .addGap(26, 26, 26))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jTextDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(jTextValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(169, 169, 169))))
        );

        jTabbedPane1.addTab("Ingresar Bono", jPanel2);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setText("Gestión de Bonos");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(265, 265, 265))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Método que se encarga de eliminar los bonos que se seleccionen en la
     * tabla.
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ArrayList<Long> seleccionados = this.getBonosSeleccionados();
        ArrayList<Long> errores = new ArrayList<Long>();
        if (seleccionados.size() > 0) {
            int confirmado = JOptionPane.showConfirmDialog(null, "¿Está seguro que quiere eliminar el/los bonos seleccionados?");
            if (JOptionPane.OK_OPTION == confirmado) {
                for (int i = 0; i < seleccionados.size(); i++) {
                    if (!controlB.eliminar(seleccionados.get(i))) {
                        errores.add(seleccionados.get(i));
                    }
                }
                if (errores.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "Eliminación exitosa", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    String s = "No se completo la asignación para: ";
                    int i = 0;
                    for (i = 0; i < errores.size() - 1; i++) {
                        s += errores.get(i) + ",";
                    }
                    s += errores.get(i);
                    JOptionPane.showMessageDialog(null, s, "Mensaje", JOptionPane.ERROR_MESSAGE);
                }

            }
            llenarTablaAsignacionBonos(Long.valueOf(labelCedulaManejar.getText()));
        } else {
            JOptionPane.showMessageDialog(null, "No se seleccionó ningún bono para eliminar.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * Método que muestra una interfaz de empleados respectiva para bono2
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Empleados empleado = new Empleados();
        empleado.init("bono2");
    }//GEN-LAST:event_jButton1ActionPerformed
    /**
     * Método que se encarga de asignar bonos a un empleado.
     *
     */
    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        this.crear();
      
    }//GEN-LAST:event_jButton7ActionPerformed

      void crear(){
    try {
            ocultarLabels();
            String descripcion = jTextDescripcion.getText();
            if (validarBono()) {
                int confirmado = JOptionPane.showConfirmDialog(null, "¿Está seguro que quiere asignar el bono?");
                if (JOptionPane.OK_OPTION == confirmado) {
                    Long cedulaEmpleado = labelCedulaIngresar.getText().trim().isEmpty() ? null : Long.valueOf(labelCedulaIngresar.getText());
                    int valor = jTextValor.getText().trim().isEmpty() ? null : numberFormat.parse(jTextValor.getText()).intValue();
                    Empleado empleado = controlE.getEmpleadoCedula(cedulaEmpleado);
                    ArrayList<Date> fechas = controlP.getPago();
                    PagoId pagoId = new PagoId(empleado.getNumEmpleado(), fechas.get(0), fechas.get(1));
                    Pago p = new Pago(pagoId);
                    Bono bono = new Bono(p, descripcion, valor);
                    controlB.insertarBono(bono);
                    JOptionPane.showMessageDialog(null, "El bono fue asignado correctamente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                }
                if (!(JOptionPane.NO_OPTION == confirmado)) {
                    limpiar();
                }
            }

        } catch (ParseException ex) {
            Logger.getLogger(ConsultarBonos.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }
    /**
     * Método que muestra una interfaz de empleados respectiva para bono1
     */
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        Empleados empleado = new Empleados();
        empleado.init("bono1");
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jTextValorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextValorKeyPressed
        int key = evt.getKeyCode();
        if (key == evt.VK_ENTER) {
            this.crear();
        }
    }//GEN-LAST:event_jTextValorKeyPressed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton9ActionPerformed

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConsultarBonos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConsultarBonos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConsultarBonos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConsultarBonos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConsultarBonos().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelCedula;
    private javax.swing.JLabel jLabelDescripcion;
    private javax.swing.JLabel jLabelValor;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextDescripcion;
    private javax.swing.JFormattedTextField jTextValor;
    private javax.swing.JLabel labelCedulaIngresar;
    private javax.swing.JLabel labelCedulaManejar;
    private javax.swing.JLabel labelCodigoC;
    private javax.swing.JLabel labelCodigoI;
    private javax.swing.JLabel labelNombreIngresar;
    private javax.swing.JLabel labelNombreManejar;
    // End of variables declaration//GEN-END:variables
}
